<?php

namespace Admin\Models\res;

use Admin\Models\BaseModel;
use Helpers\Main;

class XhresResourcepackusers extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iRPUId;

    /**
     *
     * @var integer
     */
    public $iRPId;

    /**
     *
     * @var integer
     */
    public $iSAGId;

    /**
     *
     * @var integer
     */
    public $iUserId;

    /**
     *
     * @var integer
     */
    public $iUserType;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var string
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSource("XHRes_ResourcePackUsers");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHRes_ResourcePackUsers';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhresResourcepackusers[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhresResourcepackusers
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 根据资源包ID获取关联的用户类型并返回结果数组
     *
     * @param $resPackId
     * @return array
     *
     * create by ck 20161219
     */
    public static function getContactUserType($resPackId)
    {
        $userType = self::find([
            'columns'       => 'iUserType',
            'conditions'    => 'bDelete = 0 AND iRPId = :iRPId: AND (iUserType IS NOT NULL OR iUserType <> "")',
            'bind'          => ['iRPId' => $resPackId]
        ])->toArray();
        return $userType ? array_column($userType, 'iUserType') : [];
    }

    /**
     * 根据资源包ID，用户类型及操作类型关联/取消关系
     *
     * @param $resPackId
     * @param $userType
     * @param $type
     * @return bool
     *
     * create by ck 20161219
     */
    public static function contactResPackToUserType($resPackId, $userType, $type)
    {
        if(!$userType) return true;
        if($type == 'out'){
            return self::find([
                'conditions' => 'bDelete = 0 AND iRPId = :iRPId: AND iUserType IN ({userType:array})',
                'bind'       => ['iRPId' => $resPackId, 'userType' => $userType]
            ])->delete();
        }
        $userId = Main::getLoginUserInfo('userId');
        $sql = 'INSERT INTO XHRes_ResourcePackUsers (iRPId,iUserType,bDelete,tCreateDate,iCreateUserId,tLastModifyDate,iLastModifyUserId) VALUES ';
        $nowTime = date('Y-m-d H:i:s');
        $valueArr = [];
        foreach($userType as $value){
            $valueArr[] = "({$resPackId},{$value},0,'{$nowTime}',{$userId},'{$nowTime}',{$userId})";
        }
        $sql .= implode(',', $valueArr);
        return \Phalcon\Di::getDefault()->get('db')->execute($sql);
    }

    /**
     * 根据资源包ID返回绑定的用户ID字符串
     *
     * @param $resPackId
     * @return string
     *
     * create by ck 20161221
     */
    public static function getUserIdByResPackId($resPackId)
    {
        $userId = self::find([
            'columns'    => 'iUserId',
            'conditions' => 'bDelete = 0 AND iRPId = :iRPId: AND (iUserId IS NOT NULL AND iUserId <> "")',
            'bind'       => ['iRPId' => $resPackId]
        ])->toArray();
        if(!$userId) return '';
        return implode(',', array_column($userId, 'iUserId'));
    }

    /**
     * 根据资源包ID，用户ID及操作类型关联/取消关系
     *
     * @param $resPackId
     * @param $userId
     * @param $type
     * @return bool
     *
     * create by ck 20161221
     */
    public static function contactResPackToUserId($resPackId, $userId, $type)
    {
        if(!$userId) return true;
        if($type == 'out'){
            return self::find([
                'conditions' => 'bDelete = 0 AND iRPId = :iRPId: AND iUserId IN ({userId:array})',
                'bind'       => ['iRPId' => $resPackId, 'userId' => $userId]
            ])->delete();
        }
        $nowUserId = Main::getLoginUserInfo('userId');
        $sql = 'INSERT INTO XHRes_ResourcePackUsers (iRPId,iUserId,bDelete,tCreateDate,iCreateUserId,tLastModifyDate,iLastModifyUserId) VALUES ';
        $nowTime = date('Y-m-d H:i:s');
        $valueArr = [];
        foreach($userId as $value){
            $valueArr[] = "({$resPackId},{$value},0,'{$nowTime}',{$nowUserId},'{$nowTime}',{$nowUserId})";
        }
        $sql .= implode(',', $valueArr);
        return \Phalcon\Di::getDefault()->get('db')->execute($sql);
    }

    /**
     * 根据资源包ID返回绑定的应用组ID字符串
     *
     * @param $resPackId
     * @return string
     *
     * create by ck 20161221
     */
    public static function getAppGroupIdByResPackId($resPackId)
    {
        $userId = self::find([
            'columns'    => 'iSAGId',
            'conditions' => 'bDelete = 0 AND iRPId = :iRPId: AND (iSAGId IS NOT NULL AND iSAGId <> "")',
            'bind'       => ['iRPId' => $resPackId]
        ])->toArray();
        if(!$userId) return '';
        return implode(',', array_column($userId, 'iSAGId'));
    }

    /**
     * 根据资源包ID，应用组ID及操作类型关联/取消关系
     *
     * @param $resPackId
     * @param $groupId
     * @param $type
     * @return bool
     *
     * create by ck 20161221
     */
    public static function contactResPackToAppGroup($resPackId, $groupId, $type)
    {
        if(!$groupId) return true;
        if($type == 'out'){
            return self::find([
                'conditions' => 'bDelete = 0 AND iRPId = :iRPId: AND iSAGId IN ({groupId:array})',
                'bind'       => ['iRPId' => $resPackId, 'groupId' => $groupId]
            ])->delete();
        }
        $nowUserId = Main::getLoginUserInfo('userId');
        $sql = 'INSERT INTO XHRes_ResourcePackUsers (iRPId,iSAGId,bDelete,tCreateDate,iCreateUserId,tLastModifyDate,iLastModifyUserId) VALUES ';
        $nowTime = date('Y-m-d H:i:s');
        $valueArr = [];
        foreach($groupId as $value){
            $valueArr[] = "({$resPackId},{$value},0,'{$nowTime}',{$nowUserId},'{$nowTime}',{$nowUserId})";
        }
        $sql .= implode(',', $valueArr);
        return \Phalcon\Di::getDefault()->get('db')->execute($sql);
    }

}
